import { BaseWindow } from "../../../../component/window/BaseWindow";
import { IStallBuyWindow } from "../interfaces/IStallBuyWindow";
import { ObjCreate } from "../../common/ObjCreate";
import { Language } from "../../../../../extend/language/Language";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { NumInput } from "../../common/NumInput";
import { SellItemData } from "../defin/SellItemData";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { BuyItemData } from "../defin/BuyItemData";
import { ItemData } from "../../../resource/info/ItemData";
import { ColorConfig } from "../../../resource/ColorConfig";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ColorInfo } from "../../../resource/info/ColorInfo";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GButton } from "../../../../../com/mui/controls/GButton";
//class BuyWindow
    
    export  class BuyWindow extends BaseWindow implements IStallBuyWindow
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            this.initView();
            this.addListeners();
            return;
        }

        /* internal  */initView(): void
        {
            this.setSize(288, 140);
            this.titleHeight = 25;
            var loc1=ObjCreate.createBg(17, 36, 254, 65, this);
            var loc2=ObjCreate.createTextField(Language.getString(40835), 70, 43, 70, 20, this, GlobalStyle.textFormat2);
            this._tfPrice = ObjCreate.createTextField("", 140, 43, 60, 20, this);
            this._bmpIcon = new Object /* flash.display.Bitmap */();
            this._bmpIcon.x = 200;
            this._bmpIcon.y = 43;
            this.addChild(this._bmpIcon);
            var loc3=ObjCreate.createTextField(Language.getString(40836), 70, 68, 70, 20, this, GlobalStyle.textFormat2);
            this._numInput = new NumInput();
            this._numInput.y = 68;
            this._numInput.x = 135;
            this.addChild(this._numInput);
            this._btnOK = ObjCreate.createGButton(Language.getString(40833), 85, 106, 47, 22, this);
            this._btnCancel = ObjCreate.createGButton(Language.getString(40834), 158, 106, 47, 22, this);
            return;
        }

        /* internal  */addListeners(): void
        {
            this._btnOK.addEventListener(flash.events.MouseEvent.CLICK, this.okHandler);
            this._btnCancel.addEventListener(flash.events.MouseEvent.CLICK, this.cancelHandler);
            this._numInput.addEventListener(flash.events.Event.CHANGE, this.onNumChange);
            return;
        }

        /* internal  */onNumChange(arg1: Object /* flash.events.Event */): void
        {
            this.updatePrice();
            return;
        }

        /* internal  */okHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            loc1 = this._itemData.uid;
            loc2 = this._numInput.currentNum;
            loc4 = this._itemData.itemExInfo.stall_coin;
            loc3 = this._itemData.itemExInfo.stall_gold;
            var loc5=new SellItemData(loc1, loc4, loc3, loc2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.StallBuyItem, new BuyItemData(loc1, loc2, loc4, loc3)));
            this.hide();
            return;
        }

        /* internal  */cancelHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.hide();
            return;
        }

        public set data(arg1: Object)
        {
            var loc1=null;
            this._itemData = null;
            if (typeof arg1 === "itemdata") 
            {
                this._itemData = arg1 as ItemData;
                loc1 = this._itemData.itemInfo.item.name + (this._itemData.itemAmount > 1 ? "X" + this._itemData.itemAmount.toString() : "");
                this.changeTitle(loc1, ColorConfig.instance.getItemColor(this._itemData.itemInfo.item.color));
                this._numInput.maxNum = this._itemData.itemAmount;
                this.updatePrice();
            }
            return;
        }

        /* internal  */updatePrice(): void
        {
            if (!this._itemData) 
            {
                return;
            }
            var loc1=this._itemData.itemExInfo.stall_gold;
            var loc2=this._itemData.itemExInfo.stall_coin;
            var loc3=this._numInput.currentNum;
            if (loc1 > 0) 
            {
                this._tfPrice.text = (loc1 * loc3).toString();
                this._bmpIcon.bitmapData = GlobalClass.getBitmapData("Yuanbao_small");
            }
            else if (loc2 > 0) 
            {
                this._tfPrice.text = (loc2 * loc3).toString();
                this._bmpIcon.bitmapData = GlobalClass.getBitmapData("Tongqian_small");
            }
            return;
        }

        /* internal  */changeTitle(arg1: string, arg2: ColorInfo): void
        {
            if (!this._titleSp) 
            {
                this._titleSp = new Object /* flash.display.Sprite */();
            }
            if (!this._titleLable) 
            {
                this._titleLable = ObjCreate.createTextField("");
            }
            this._titleLable.y = 4;
            this._titleSp.addChild(this._titleLable);
            this._titleLable.mouseEnabled = false;
            this._titleLable.selectable = false;
            this._titleLable.filters = [FilterConst.glowFilter];
            this._titleLable.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._titleLable.htmlText = HTMLUtil.addColor(arg1, arg2.color);
            title = this._titleSp;
            return;
        }

        public static getInstance(): BuyWindow
        {
            if (!BuyWindow._instance) 
            {
                BuyWindow._instance = new BuyWindow();
            }
            return BuyWindow._instance;
        }

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */_btnOK: GButton;

        /* internal */ /* var */_btnCancel: GButton;

        /* internal */ /* var */_numInput: NumInput;

        /* internal */ /* var */_tfPrice: Object /* flash.text.TextField */;

        /* internal */ /* var */_bmpIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_titleLable: Object /* flash.text.TextField */;

        /* internal */ /* var */_titleSp: Object /* flash.display.Sprite */;

        /* internal */ static /* var */_instance: BuyWindow;
    }
